//
//  JCCCollectViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/12/1.
//  Copyright © 2024 shipin. All rights reserved.
//

import CommonTools
import UIKit

class JCCCollectModel {
    var code: String = ""
    var gcode: String = ""
    var name:String = ""
    var score:Int = 0
    var grade:String = ""
    var data_image:String = ""
    var isSelect:Bool = false
    init(dict: [String: Any]) {
        self.code = dict["code"] as? String ?? ""
        self.gcode = dict["gcode"] as? String ?? ""
        self.name = dict["name"] as? String ?? ""
        self.score = dict["score"] as? Int ?? 0
        self.grade = dict["grade"] as? String ?? ""
        self.data_image = dict["data_image"] as? String ?? ""
    }
}

class JCCCollectTableViewCell: UITableViewCell {
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var numberLabel: UILabel!
    @IBOutlet weak var gradeLabel: UILabel!
    
    @IBOutlet weak var gradeMarkView: UIView!
    @IBOutlet weak var selectBtn: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        leftImageView.layer.cornerRadius = 12
        bgView.layer.cornerRadius = 7
        gradeMarkView.cut(withRadius: 6.5)
        contentView.backgroundColor = .blackf6
    }
    
    override func layoutSubviews() {
        
        
    }
    
}


class JCCCollectViewController: BaseViewController {
    
    @IBOutlet weak var managerBtn: UIButton!
    
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var bottomHeight: NSLayoutConstraint!
    @IBAction func cancelAction(_ sender: Any) {
        EZLoadingActivity.show("正在取消...",disableUI: true)
        let deleteModel = models.filter { $0.isSelect }
        JCCNet.setFavorite(isAdd: false, gcodes: deleteModel.map{$0.gcode}) { (model, error) in
            self.isEdit = false
            self.updateUI()
            if(model?.isSuccess == true){
                EZLoadingActivity.hide()
                self.getData()
            }else {
                EZLoadingActivity.Settings.SuccessText = model?.info ?? ""
                EZLoadingActivity.hide(true, animated: false)
            }
        }
    }
    @IBOutlet weak var selectCouttLabel: UILabel!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var collTableView: UITableView!
    var isEdit: Bool = false
    var models:[JCCCollectModel] = []
    
    @IBAction func editAction(_ sender: Any) {
        self.isEdit = !self.isEdit
        self.updateUI()
        self.collTableView.reloadData()
    }
    
    override func viewDidLoad() {
        setTitle("收藏")
        bottomHeight.constant = 0
        selectCouttLabel.text = "已选:\(0)"
        view.backgroundColor = .white
        collTableView.backgroundColor = .blackf6
        self.collTableView.delegate = self
        self.collTableView.dataSource = self
        self.collTableView.separatorStyle = .none
        self.cancelBtn.layer.cornerRadius = 19
        getData()
    }
    override func viewWillAppear(_ animated: Bool) {
        
//        self.navigationController?.navigationBar.isHidden = true
    }
    func updateUI () {
        self.bottomView.isHidden = !self.isEdit
        self.managerBtn.setTitle(!self.isEdit ? "管理":"完成", for: .normal)
        bottomHeight.constant = isEdit ? 60 : 0
    }
    override func viewWillDisappear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = false
    }

    
    @IBAction func popAction(_ sender: Any) {
        self.popAction()
    }
    func getData() {
        JCCNet.getFavoriteList(completion: {
            model,error in
            let dicts = model?.result as! [Any]
            let models:[JCCCollectModel] = dicts.map { JCCCollectModel(dict: $0 as! [String : Any]) }
            self.models = models
            self.countLabel.text = "已收藏\(models.count)个"
            self.collTableView.reloadData()
            
        })
    }
}

extension JCCCollectViewController {
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:JCCCollectTableViewCell = tableView.dequeueReusableCell(withIdentifier: "collectCell", for: indexPath) as! JCCCollectTableViewCell
        let model:JCCCollectModel = models[indexPath.row]
        cell.titleLabel.text = model.name
        cell.selectBtn.isHidden = !self.isEdit
        cell.selectBtn.setImage( UIImage.init(named:model.isSelect == true ? "jcc_select":"jcc_no_select" ), for: .normal)
        cell.numberLabel.text = "条码编号:\(model.code)"
        cell.gradeLabel.text = "\(model.grade)级"
        cell.leftImageView.setImageUrl(url: model.data_image)
        
        let isSelectCount = models.filter { $0.isSelect }.count
        selectCouttLabel.text = "已选:\(isSelectCount)"
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.models.count
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if isEdit {
            let model = models[indexPath.row]
            model.isSelect = !model.isSelect
            tableView.reloadRow(at: indexPath, with: .none)
        }
        else {
            
        }
    }
}
